/**
 * 視圖中介器, 负责loading的控制
 * 
 * @author LuoDong
 * @email luo4521@online.net.tw
 * 2010-1-29 9:52
 */
package com.addcn.texas.view
{

	import com.addcn.common.event.ParamEvent;
	import com.addcn.texas.view.components.PopupWindow;
	import flash.display.*;
	import flash.events.Event;
	
	import com.addcn.fog.view.*;
	import com.addcn.fog.control.*;
	import com.addcn.fog.model.*;
	
	import com.addcn.texas.Main;
	import com.addcn.texas.control.*
	import com.addcn.texas.view.*;
	import com.addcn.texas.model.*;
	import com.addcn.texas.utils.MsgTable;
	import com.addcn.common.utils.AppLibary;
	
		
	public class PopupView extends View
	{		
		public static const SHOW_ALERT_BOX : String = "showAlerBox";
		public static const HIDE_NOTICE : String = "hideNotice";
		public static const SHOW_NOTICE : String = "showNotice";
		public static const SHOW_BECLOUD : String = "showBecloud";
		public static const HIDE_BECLOUD : String = "hideBecloud";
		public static const SHOW_CARRY_BOX:String = "showCarryBox";		
		
		protected var _container : Object;
		private var _msg_table :MsgTable =  MsgTable.getInstance();
		private var _components: Object;
				
		public function PopupView(target : Object) {
			_container = target;
			_inlitalize();
		}			
		
		private function _inlitalize() : void
		{
			this._components = { };
		}
		
		/**
		 * 該視圖中介器需要偵聽的通知
		 */
		override public function notificationsList():void 
		{
			notifications = [
							Main.APP_STARTUP,
							RoomUserModel.EXCHANGE_CHIP,
							SHOW_ALERT_BOX,
							SHOW_NOTICE,
							HIDE_NOTICE,
							SHOW_BECLOUD,
							HIDE_BECLOUD,
							SHOW_CARRY_BOX,
							StartupModel.CONNECTION_LOST,							
							];
		}
		
		override public function onDataChanged(r_notification:String, r_data:Object = null):void {					
			switch (r_notification) {
				case Main.APP_STARTUP:

					break;				
				case RoomUserModel.EXCHANGE_CHIP:
					_showCarryBox(r_data);
					break;
				case SHOW_ALERT_BOX:					
					this._showAlert(r_data);
					break;				
				case SHOW_NOTICE:
					_notice(String(r_data));
					break;				
				case HIDE_NOTICE:
					_hideNotice();
					break;
				case SHOW_BECLOUD:
					var _alpha : Number = 0.5;
					if (r_data != null && r_data["alpha"] != null) {
						_alpha = Number(r_data.alpha);
					}
					_showBecloud(_alpha);
					break;
				case HIDE_BECLOUD:
					_hideBecloud();
					break;
				case SHOW_CARRY_BOX:
				//	_showCarryBox(r_data as Array);
					break;
				case StartupModel.CONNECTION_LOST:
					_connectionLost(r_data.code);
					break;
			}			
		}
		
		/**
		 * 显示提示框
		 * @param	r_code String 消息编码
		 */
		private function _notice(r_code : String) : void
		{
			_hideNotice();
			var _class = AppLibary.getInstance().getClass("com.addcn.texas.view.components.PopupWindow");			
			if ( _class != null) {
				_showBecloud();
				var _alert_mc  = new _class(this._container, _class.NOTICE);
				_alert_mc.name = "noticeWin";
				_alert_mc.msg = _msg_table.msg(r_code);
				_alert_mc.show();
			}
		}
		
		private function _hideNotice() : void
		{			
			var _mc = this._container.getChildByName("noticeWin");
			if (_mc != null) {
				_mc.remove();
			}
			_hideBecloud();
		}
		
		/**
		 *  顯示彈出框
		 * 
		 * @param   r_data  Object 
		 * 	r_data["type"]  	String  彈出框類型 alert,select
		 * 	r_data["msg_code"]  String   消息编码
		 * 	r_data["fun"] 		array  { Function1 ,Funtion2},如果 r_type 為 alert, Funtion2可為空
		 * 	r_data["params"] 	array  { params1 ,params2},如果 r_type 為 alert, params2可為空
		 */
		private function _showAlert(r_data : Object) : void 
		{
			var _type = r_data["type"] ? r_data["type"] : "alert";
			var _msg : String = _msg_table.msg(r_data["code"]);
			var OKClickHandler = function() {
				if (r_data["fun"]) {
					var _fun : Array = r_data["fun"];
					if ((r_data["params"] as Array) && r_data["params"].length > 0 ) {
						var _ok_params =  r_data["params"].shift();						
						_fun.shift().call(_ok_params);
					}else {
						_fun.shift().call();
					}					
				}
				_alert_mc.removeEventListener(_class.OK_EVENT, OKClickHandler);
				_alert_mc.removeEventListener(_class.NO_EVENT, NOClickHandler);
				_hideBecloud();
			}
			var NOClickHandler = function() {
				if (r_data["fun"]) {
					var _fun : Array = r_data["fun"];
					if ((r_data["params"] as Array) && r_data["params"].length > 0 ) {
						var _ok_params =  r_data["params"].shift();						
						_fun.shift().call(_ok_params);
					}else {
						_fun.shift().call();
					}					
				}				
				_alert_mc.removeEventListener(_class.OK_EVENT, OKClickHandler);
				_alert_mc.removeEventListener(_class.NO_EVENT, NOClickHandler);
				_hideBecloud();
			}
			_showBecloud();
			var _class = AppLibary.getInstance().getClass("com.addcn.texas.view.components.PopupWindow");			
			if( _class != null){
				var _alert_mc  = new _class(this._container, _type);
				_alert_mc.msg = _msg;
				_alert_mc.addEventListener(_class.OK_EVENT, OKClickHandler);
				_alert_mc.addEventListener(_class.NO_EVENT, NOClickHandler);
				_alert_mc.show();
			}		
		}		
		
		/**
		 * 显示挡板
		 * @param	r_alpah Number 挡板透明度
		 */
		function _showBecloud(r_alpah : Number = 0.5) : void
		{
			var _class = AppLibary.getInstance().getClass("com.addcn.texas.view.components.Becloud");			
			if (_class != null && this._container.getChildByName("Becloud") == null) {
				var _becloud = new _class(this._container);
				_becloud.name = "Becloud";
				_becloud.setAlpha(r_alpah);
				_becloud.show();
				this._container.parent.stage.focus = null;
			}
		}

		function _hideBecloud() : void {
			var _becloud = this._container.getChildByName("Becloud");
			if (_becloud != null) {
				_becloud.hide();
			}
		}
		
		/**
		 * 籌碼兌換框
		 * @param	r_data
		 */
		private function _showCarryBox(r_data : Object) : void
		{
			if (this._components["CarryBox"] != null) return;
			var _class = AppLibary.getInstance().getClass("com.addcn.texas.view.components.CarryBox");
			if (_class) {
				var _mc = new _class(this._container);
				_mc.initialize(r_data);
				_mc.name = r_data["sit"];
				this._showBecloud(0);		
				this._components["CarryBox"] = _mc;		
				_mc.addEventListener(_class.BUY_EVENT, _buyHandler);
				_mc.addEventListener(_class.CLOSE_EVENT, _carryCloseHandler);
				_mc.show();
			}			
		}
		
		private function _buyHandler(evt : ParamEvent) : void
		{			
			this._hideBecloud();
			this._components["CarryBox"] = null;
			if (!Controller.getInstance().hasCommand(userOperationCommand)) {
				Controller.getInstance().addCommand(userOperationCommand);
			}
			var _sit : int = parseInt(evt.target.name);
			var _model : RoomUserModel = ModelLocator.getInstance().getModel(RoomUserModel);			
			sendNotification(userOperationCommand, { type:"sit", sit:_sit ,chip: evt.params.chip} );								
		}
				
		private function _carryCloseHandler(evt : Event) : void
		{			
			this._hideBecloud();
			this._components["CarryBox"] = null;
		}
		
		private function _connectionLost(r_code :String) : void {
			_notice(r_code);
			sendNotification(LoadingView.HIDE_LOADING);
			_showBecloud();
		}
		
	}
	
}
